import request from "@/utils/request";

// 获取商品分类
export const getCostumeCategory = () => {
  return request({
    url: "/costumeCategories/getAllCategories",
    method: "get"
  });
};

// 获取所有店铺信息
export const getAllStores = () => {
  return request({
    url: "/costumes/getAllStores",
    method: "get"
  });
};

// 上传图片
export const uploadImage = (file: File) => {
  const formData = new FormData();
  formData.append("file", file);
  return request.post("/upload", formData, {
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

// 将当前子服饰设置为轮播图
export const setCostumeAsCarousel = (data: any) => {
  return request({
    url: `/costumeCategories/update`,
    method: "post",
    data
  });
};

// 更新分类轮播状态
export const updateCostumeCategory = (data: {
  pid: string;
  isBanner?: number;
  isRecommend?: number;
}) => {
  return request.post("/costumeCategories/update", data);
};

// 添加分类
export const addCostume = (formData: FormData, params: { id: string }) => {
  return request.post(`/costumes/addImages/${params.id}`, formData, {
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

/**
 * 添加分类
 * @param formData 包含图片的FormData对象，文件字段名为'files'
 * @param params 分类参数，包括name(分类名称)、descs(分类描述)和可选的parentId(父分类ID)
 * @returns Promise 返回添加结果
 */
export const addCategory = (
  formData: FormData,
  params: { name: string; descs: string; parentId?: string }
) => {
  const url = params.parentId
    ? `/costumeCategories/sendData?name=${params.name}&descs=${params.descs}&parentId=${params.parentId}`
    : `/costumeCategories/sendData?name=${params.name}&descs=${params.descs}`;

  return request.post(url, formData, {
    headers: {
      "Content-Type": "multipart/form-data"
    }
  });
};

// 删除分类
export const deleteCategory = (id: string) => {
  return request.post(`/costumeCategories/delete/${id}`);
};

// 获取商品分类的子分类
export const getSubCategories = (id: string) => {
  return request.get(`/costumes/items/${id}`);
};
